import axios from '../../utils/request'
const moduleURL = '/student'
const state = {
  listPageData:{
    currentPage:1,
    total:1,
    rowsList:[]
  }

}

//方法参考地址
//https://www.jb51.net/article/261401.htm
const mutations = {

  //保存列表信息
  setListPageData: (state, data) => {
    state.listPageData = data
  },

}
const actions = {

  /**
   * 获取分页数据
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  getListPageData: function ({commit}, params) {

    return new Promise((resolve, reject) => {
        // request({
//       url: url,
//       method: 'get',
//       params: params
//     })
      // const params = {page:1,pageSize:1}; // 参数
      axios.get(moduleURL+'/listPage',{params:params}).then((res) => {
          // console.log('find ' +JSON.stringify(res))
          const dataValue = res.data;
          const listPageData = {}
          listPageData.currentPage = dataValue.page
          listPageData.total = dataValue.total
          listPageData.rowsList = dataValue.rows
          commit("setListPageData", listPageData)
          // resolve()
        resolve(res)
        }).catch((error) => {
          reject(error)
        })

    }).catch(error=>{
      console.log(error)
    })

  },

  /**
   * 更新学生数据
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  updateObject: function ({commit}, from) {
    return new Promise((resolve, reject) => {
      // console.log('from ' +JSON.stringify(from))
      axios.post(moduleURL+'/saveOrUpdate', from).then((res) => {
        resolve(res)
      }).catch((error) => {
        reject(error)
      })

    }).catch(error=>{
      console.log(error)
    })

  },

  /**
   * 删除学生数据
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  deleteObject: function ({commit}, ids) {
    return new Promise((resolve, reject) => {
      axios.delete(moduleURL+'/deleteByIds?ids='+ids).then((res) => {
        resolve(res)
      }).catch((error) => {
        reject(error)
      })
    }).catch(error=>{
      console.log(error)
    })

  },


}


export default {
  namespaced: true,
  state,
  mutations,
  actions
}
